package com.yuandian.management.utils;

/**
 * 迁移bpm3.0
 */
public class Util {

	public static int getInteger(byte[] data, int offset, int length) {
		int r = 0;
		int i;

		if (offset<0) offset=0;
		if (length>4) length=4;
		if (offset+length>data.length) return 0;
		for (i=0;i<length;i++) {
			r += (data[offset+i] & 0xFF)<<(8*i);
		}
		return r;
	}



	public static long getLong(byte[] data, int offset, int length) {
		long r = 0;
		int i;

		if (offset<0) offset=0;
		if (length>8) length=8;
		if (offset+length>data.length) return 0;
		for (i=0;i<length;i++) {
			r += (data[offset+i] & 0xFFl)<<(8*i);
		}
		return r;
	}

	public static String getString(byte[] data, int offset, int length) {
		return getString(data, offset, length, "utf-8");
	}

	public static String getString(byte[] data, int offset, int length, String encoding) {
		String r = null;
		int i;
		try {
			for (i=0;i<length;i++) if (data[offset+i]==0) break;
			r = new String(data, offset, i, encoding);
		} catch (Exception ex) {
		}

		return r;
	}

	public static String getMAC(byte[] data, int offset) {
		return String.format("%02x:%02x:%02x:%02x:%02x:%02x", 0xFF & data[offset], 0xFF & data[offset+1], 0xFF & data[offset+2], 0xFF & data[offset+3], 0xFF & data[offset+4], 0xFF & data[offset+5]);
	}

	public static String getIP(byte[] data, int offset) {
		return String.format("%d.%d.%d.%d", 0xFF & data[offset+3], 0xFF & data[offset+2], 0xFF & data[offset+1], 0xFF & data[offset]);
	}

	public static String getIP(long ip) {
		String r = "";
		r = String.valueOf(ip % 256);
		ip = ip / 256;
		r = String.valueOf(ip % 256) + "." + r;
		ip = ip / 256;
		r = String.valueOf(ip % 256) + "." + r;
		ip = ip / 256;
		r = String.valueOf(ip % 256) + "." + r;
		return r;
	}
	// 将127.0.0.1形式的IP地址转换成十进制整数，这里没有进行任何错误处理
	public static long ipToLong(String strIp) {
		long[] ip = new long[4];
		// 先找到IP地址字符串中.的位置
		int position1 = strIp.indexOf(".");
		int position2 = strIp.indexOf(".", position1 + 1);
		int position3 = strIp.indexOf(".", position2 + 1);
		// 将每个.之间的字符串转换成整型
		ip[0] = Long.parseLong(strIp.substring(0, position1));
		ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
		ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
		ip[3] = Long.parseLong(strIp.substring(position3 + 1));
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}

	public static int ipToInt(String strIp) {
		int[] ip = new int[4];
		// 先找到IP地址字符串中.的位置
		int position1 = strIp.indexOf(".");
		int position2 = strIp.indexOf(".", position1 + 1);
		int position3 = strIp.indexOf(".", position2 + 1);
		// 将每个.之间的字符串转换成整型
		ip[0] = Integer.parseInt(strIp.substring(0, position1));
		ip[1] = Integer.parseInt(strIp.substring(position1 + 1, position2));
		ip[2] = Integer.parseInt(strIp.substring(position2 + 1, position3));
		ip[3] = Integer.parseInt(strIp.substring(position3 + 1));
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}

	// 将十进制整数形式转换成127.0.0.1形式的ip地址
	public static String longToIP(long longIp) {
		StringBuffer sb = new StringBuffer("");
		// 直接右移24位
		sb.append(String.valueOf((longIp >>> 24)));
		sb.append(".");
		// 将高8位置0，然后右移16位
		sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
		sb.append(".");
		// 将高16位置0，然后右移8位
		sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
		sb.append(".");
		// 将高24位置0
		sb.append(String.valueOf((longIp & 0x000000FF)));
		return sb.toString();
	}

	public static byte[] getMacBytes(String mac) {
		byte[] macBytes = new byte[6];
		String[] strArr = mac.split("-");

		for (int i = 0; i < strArr.length; i++) {
			int value = Integer.parseInt(strArr[i], 16);
			macBytes[i] = (byte) value;
		}
		return macBytes;
	}

	// 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
	public static int toInt(byte[] bRefArr) {
		int iOutcome = 0;
		byte bLoop;

		for (int i = 0; i < bRefArr.length; i++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * i);

		}

		return iOutcome;
	}

}
